package com.gxa.modules.app.hospitalization.rechargeAndExpenses.controller;


import com.gxa.common.utils.Result;
import com.gxa.modules.app.hospitalization.persional.service.PatientService;
import com.gxa.modules.app.hospitalization.rechargeAndExpenses.service.InpatientDetailAppService;
import com.gxa.modules.app.outpatient.onlinepay.dto.PayDetail;
import com.gxa.modules.model.dto.*;
import com.gxa.modules.model.entity.fee.Patient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 住院充值记录表 前端控制器
 * </p>
 *
 * @author lst
 * @since 2022-11-13
 */
@Api(tags = "住院充值和住院费用和个人中心（住院人管理）")
@RestController
@RequestMapping("/hospitalization/msg")
@Slf4j
public class RechargeController {

    @Autowired
    private InpatientDetailAppService inpatientDetailAppService;

    @Autowired
    private PatientService patientService;

    //时间转换器
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation(value = "住院人管理&住院充值&住院费用（选择住院人员）------查询所有住院人的信息", response = InpatientDto.class)
    @ApiImplicitParam(paramType = "query",name = "userId",value ="当前登陆用户的id",dataType ="int")
    @GetMapping
    public Result queryByUserId(Integer userId){

        List<InpatientDto> list = inpatientDetailAppService.listAll(userId);

        return new Result().ok(list);

    }

    @ApiOperation(value = "住院充值（住院人信息）&住院费用（住院记录）------住院人的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "patientId",value ="患者的id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "useId",value ="登陆用户的id",dataType ="int"),
    })
    @GetMapping("/query")
    public Result<InpatientDetailDto> queryByPatientId(Integer patientId, Integer useId){
        InpatientDetailDto list = inpatientDetailAppService.queryById(patientId, useId);

        return new Result<InpatientDetailDto>().ok(list);
    }

    @ApiOperation(value = "住院充值（住院充值，住院充值记录）------查询住院卡的金额")
    @ApiImplicitParam(paramType = "query",name = "id",value ="患者的id",dataType ="int")
    @GetMapping("/card")
    public Result<CardDto> queryByPatientIdToCard (Integer id){
        CardDto cardDto = inpatientDetailAppService.queryByPatientIdToCard(id);
        return new Result<CardDto>().ok(cardDto);
    }

    @ApiOperation(value = "住院充值（住院充值记录） 查询住院充值记录", response = PayDetail.class)
    @GetMapping("/recharge")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id",value ="患者的id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value ="开始时间",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value ="结束时间",dataType ="Date"),
    })
    public Result queryByTimeToRecharge(Date startTime, Date endTime, Integer id){
        List<PayDetail> list = inpatientDetailAppService.queryByTime(startTime, endTime, id);

        return new Result().ok(list);
    }




    @ApiOperation(value = "住院费用（住院记录）------查询住院记录", response = HospitalizationDto.class)
    @GetMapping("/hospitalization")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id",value ="患者的id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "startTime",value ="开始时间",dataType ="Date"),
            @ApiImplicitParam(paramType = "query",name = "endTime",value ="结束时间",dataType ="Date"),
    })
    public Result queryByTimeToHospitalization(Date startTime, Date endTime, Integer id){
        List<HospitalizationDto> list = inpatientDetailAppService.queryByTimeToHospitalization(startTime, endTime, id);
        return new Result().ok(list);
    }


    @ApiOperation(value = "住院费用（费用详情）---根据住院缴费记录id查询对应患者的信息")
    @GetMapping("/idcard")
    @ApiImplicitParam(paramType = "query",name = "id",value ="住院消费记录的id",dataType ="int")
    public Result<IdCardDto> queryByIdToMsg(Integer id){
        IdCardDto idCardDto = inpatientDetailAppService.queryByIdToMsg(id);

        return new Result<IdCardDto>().ok(idCardDto);
    }


    @ApiOperation(value = "住院费用（费用详情）----住院消费记录id和患者id查询住院的时间段和总金额")
    @GetMapping("/dayfees")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "住院消费记录的id", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "patientId", value = "患者的id", dataType = "int"),
    })
    public Result<FreeAndDateDto> queryByDateTofees(Integer id, Integer patientId){
        FreeAndDateDto dto = inpatientDetailAppService.queryByTimeToMoney(patientId, id);

        return new Result<FreeAndDateDto>().ok(dto);
    }

    @ApiOperation(value = "住院人管理(确认住院人信息)----------------新增住院号")
    @ApiImplicitParam(paramType = "body",name = "patientId",value ="就诊人id",dataType ="int")
    @PostMapping
    public Result<String> add(@RequestBody Integer patientId){
        String add = inpatientDetailAppService.add(patientId);
        return new Result<String>().ok(add);
    }

    @ApiOperation(value = "住院人管理(绑定住院号)------查询当前账户所有的就诊人", response = Patient.class)
    @ApiImplicitParam(paramType = "query",name = "useId",value ="登陆账号的id",dataType ="int")
    @GetMapping("/patient")
    public Result queryByUserIdToPatient(@RequestParam Integer useId){
        List<Patient> list = patientService.query(useId);
        return new Result().ok(list);
    }


}
